<?php
/**
 * Created by Benjamin.
 * Date: 2020/5/13
 * Time: 14:21
 */

namespace App\Model;


use App\Services\HelpService;
use EasySwoole\EasySwoole\Config;
use EasySwoole\Jwt\Jwt;
use EasySwoole\ORM\AbstractModel;
class UserModel extends AbstractModel
{
    protected $tableName = 'users';
    protected $primaryKey = 'id';
    const USER_TYPE_NORMAL = 1;//普通用户
    const USER_TYPE_SERVICE = 2;//客服
    const USER_TYPE_ADMIN = 3;//管理员

    const USER_STATUS_DEL = 0;#删除状态
    const USER_STATUS_NORMAL = 1;#正常
    const USER_STATUS_LOCK = 2;#冻结状态
    /*
     * 短信登陆
     */
    function login($countryCode,$mobile)
    {
        $info = $this->get(['mobile'=>$mobile,'country_code'=>$countryCode]);

        if(empty($info)){
            $info['nickname'] = $mobile;
            $info['country_code'] = $countryCode;
            $info['mobile'] = $mobile;
            $info['imid']  = HelpService::createImid();
            $regRet = $this->createUser($info);

           if( $regRet['error']!=0){
               return false;
           }
            $info = $this->get(['mobile'=>$mobile,'country_code'=>$countryCode]);
            $info['is_new'] = 1;
        }else{
            $info['is_new'] = 0;
        }

        $jwtObject = Jwt::getInstance()
        //    ->setSecretKey('easyswoole') // 秘钥
            ->publish();
        $jwtObject->setAlg('HMACSHA256'); // 加密方式
        $jwtObject->setAud('user'); // 用户
        $jwtObject->setExp(time()+360000); // 过期时间
        $jwtObject->setIat(time()); // 发布时间
        $jwtObject->setIss('easyswoole'); // 发行人
        $jwtObject->setJti(md5(time())); // jwt id 用于标识该jwt
        $jwtObject->setNbf(time()); // 在此之前不可用
        $jwtObject->setSub('用户信息'); // 主题
        // 自定义数据
        $jwtObject->setData( $info);

        // 最终生成的token
        $token = $jwtObject->__toString();
        $info['token'] = $token;
        $info['access_token'] = $token;
        return $info;
    }

    /**
     * 注册账号
     */
    public function createUser(array $userData){
        //是否注册过
        $userData['imid'] = HelpService::createImid();
        $userData['created_at'] = HelpService::msectimeToStr(HelpService::msectime());
        $userData['updated_at'] = HelpService::msectimeToStr(HelpService::msectime());
        if($this->data($userData,false)->save()){
            return ['error'=>0,'注册成功'];
        }
        return ['error'=>1,'注册失败'];
    }

    /**通过手机号获取用户信息
     * @param $countryCode
     * @param $mobile
     * @return UserModel|array|bool|null
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    function getOneByMobile($countryCode,$mobile){
        $info = $this->get(['mobile'=>$mobile,'country_code'=>$countryCode]);
        return $info;
    }

    /**
     * 密码加密
     */
     function pwdEncode($password){
         return md5($password.md5(Config::getInstance()->getConf('PWD_SALT')));
    }

    /**
     * @param $password密码
     * @param $encodePassword密文
     */
    function pwdCheck($password,$encodedPassword){
        if($this->pwdEncode($password)==$encodedPassword){
            return true;
        }
        return false;
    }
}